package cn.engage.product.controller;

import cn.engage.basic.util.LoginContext;
import cn.engage.org.domain.Employee;
import cn.engage.org.domain.Shop;
import cn.engage.org.service.EmployeeService;
import cn.engage.pet.domain.Pet;
import cn.engage.product.service.IProductService;
import cn.engage.product.domain.Product;
import cn.engage.product.query.ProductQuery;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import cn.engage.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;
    @Autowired
    public EmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            Long id = logininfo.getId();
            // 通过登录人信息找到登录的员工
            Employee employee = employeeService.findByLogininfoId(id);
            // 在emp中获取id
            product.setShopId(employee.getShopId());
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.add(product);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id) {
        return productService.findByIdOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){
        return productService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Long id = logininfo.getId();
        // 通过登录人信息找到登录的员工
        Employee employee = employeeService.findByLogininfoId(id);
        // 在emp中获取id
        if (employee!= null){
            if (employee.getShopId() != null){
                query.setShopId(employee.getShopId());
            }
        }
        return productService.queryByPage(query);
    }


    /* 产品下架*/
    @PostMapping("/offsale") // @RequestBody：获取请求体中的参数
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        productService.offsale(ids);
        return AjaxResult.me().success();
    }

    /* 产品上架 */
    @PostMapping("/onsale") // @RequestBody：获取请求体中的参数
    public AjaxResult onsale(@RequestBody List<Long> ids){
        productService.onsale(ids);
        return AjaxResult.me().success();
    }

    //获取用户详细信息
    @GetMapping("/getPetDetail/{id}")
    public Product getPetDetail(@PathVariable("id")Long id) {
     //   System.err.println(productService.findProdectDetail(id));
        return productService.findProdectDetail(id);
    }
}
